﻿using AnesSystem.Models;
using AnesSystem.Models.Model;
using AnesSystem.Services;
using AnesSystem.Services.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnesSystem.ViewModels
{
    public class AddLaborTemplateViewModel : BaseViewModel<AddLaborTemplateViewModel>
    {
        #region Variables
        private string _templateName;
        private bool isGeren;


        #endregion


        #region Constructor

        #endregion


        #region Properties
        public string TemplateName
        {
            get
            {
                return _templateName;
            }

            set
            {
                _templateName = value;
                RaisePropertyChanged("TemplateName");
            }
        }

        public bool IsGeren
        {
            get
            {
                return isGeren;
            }

            set
            {
                isGeren = value;
                RaisePropertyChanged("IsGeren");
            }
        }

        public string AddTemplateModel()
        {
            if (string.IsNullOrEmpty(TemplateName))
            {
                MessageBoxCommon.ShowDialog("请输入模板名称!", MessageBoxCommon.OK);
                return Constants.FAILED;
            }
            LaborTemplateModel template = new LaborTemplateModel();
            if (IsGeren)
            {
                template.UserId = DataManager.Instance.Login.Id.ToString();
            }
            else
            {
                template.UserId = Constants.TEMPLATE_DEPARTMENT_ACCOUNTID;
            }
            template.CreateTime = DateTime.Now;
            template.Name = TemplateName;
            List<PatientLaborEventModel> eventList = PatientLaborEventService.Instance.GetPatientEvents8Time(PatientLink.Id.ToString(),PatientLink.Status.InLaborTime);
            eventList.Sort((PatientLaborEventModel o1, PatientLaborEventModel o2) =>
            {
                if (o1.BeginTime == o2.BeginTime)
                {
                    if (o1.InputTime < o2.InputTime)
                    {
                        return -1;
                    }
                    else
                    {
                        return 1;
                    }
                }
                else if (o1.BeginTime < o2.BeginTime)
                {
                    return -1;
                }
                else if (o1.BeginTime > o2.BeginTime)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            });
            foreach (var item in eventList)
            {
                if (!("分娩镇痛开始").Equals(item.Name))
                {
                    PatientLaborEventModel ple = new PatientLaborEventModel();
                    ple.Name = item.Name;
                    ple.Code = item.Code;
                    template.TemplateList.Add(ple);
                }
            }
            LaborTemplateService.Instance.AddUpdate(template);
            return Constants.SUCCESS;
            
        }
        #endregion


        #region 依赖属性


        #endregion


        #region Event


        #endregion


        #region PrivateMethods


        #endregion


        #region Public/ProtectedMethods


        #endregion


        #region EventHandlers


        #endregion
    }
}
